package com.shengdong.water.code.viewmodel

import com.shengdong.water.base.BaseViewModel
import com.shengdong.water.code.bean.GovernBean
import com.shengdong.water.code.bean.NotifyMsg
import com.shengdong.water.code.bean.Outlet
import com.shengdong.water.code.bean.OutletCategoryBean
import com.shengdong.water.code.bean.PageData
import com.shengdong.water.code.bean.RealTimeBean
import com.shengdong.water.code.bean.SignInBean

import com.shengdong.water.code.repository.ApiRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
import javax.inject.Inject
import kotlin.collections.filterNot

@HiltViewModel
class PageListViewModel @Inject constructor(
    private val repository: ApiRepository
) : BaseViewModel() {

    private val _signList = MutableStateFlow<List<SignInBean>>(emptyList())
    val signList: StateFlow<List<SignInBean>> = _signList
    private var isRefreshing = false

    fun getSignList(sign: MutableMap<String, Any>, current: Int, size: Int) {

        handlePagingRequest(
            flow = repository.getSignList(sign, current, size),
            onSuccess = { it ->
                _signList.update { oldList ->
                    if (isRefreshing || current == 1) {
                        isRefreshing = false
                        it
                    } else {
                        oldList + it.filterNot { newItem ->
                            oldList.any { it.id == newItem.id }
                        }
                    }

                }
            },
            onError = { code, msg ->
                when (code) {
                    401 -> {}// 处理未授权
                    403 -> {}// 处理禁止访问
                    else -> showError(msg)
                }
            },
            onException = { e ->
                showError("发生错误: ${e.message}")
            },
        )
    }




    private val _governList =
        MutableStateFlow<List<GovernBean>>(emptyList())
    val governList: StateFlow<List<GovernBean>> = _governList

    fun getGovernList(current: Int, size: Int) {

        handlePagingRequest(
            flow = repository.getGovernList(current, size),
            onSuccess = { it ->
                _governList.update { oldList ->
                    if (isRefreshing || current == 1) {
                        isRefreshing = false
                        it
                    } else {
                        oldList + it.filterNot { newItem ->
                            oldList.any { it.id == newItem.id }
                        }
                    }

                }
            },
            onError = { code, msg ->
                when (code) {
                    401 -> {}// 处理未授权
                    403 -> {}// 处理禁止访问
                    else -> showError(msg)
                }
            },
            onException = { e ->
                showError("发生错误: ${e.message}")
            },
        )
    }

}